package utilidades

import java.util.ArrayList
import java.util.List

class Utils {
	
	
	def <T> takeRandom(List<T> list, Integer count) {
		val List<T> newList = new ArrayList<T>
		while(newList.size < count) {
				newList.addIfNotExist(list.get(calculeRandomIndex(list))) 
		}
		return newList	
	}
	
	def <T> calculeRandomIndex(List<T> list) {
		Math.floor(Math.random * list.size).intValue
	}

	def <T> addIfNotExist(List<T> list, T element) {
		if (!list.contains(element)) list += element
	}
	
	def probability(Integer porcentaje) {
		Math.random*100 <= porcentaje
	}
	
	def <T> flatten(List<T> list, List<? extends T> listToFlatten) {
		list += listToFlatten
		list
	}
	
	def <T> flattenIf(List<T> list, List<? extends T> listToFlatten, boolean condition) {
		if (condition) list += listToFlatten
		list	
	}	
	
}